<?php
/**
 * CakeCMS后台 - 会员model
 * 
 * @author zzdboy
 * @version 1.0
 */
class User extends AppModel {
	public $name = 'User';
	public $useTable = 'user';
	public $primaryKey = 'uid';
	
	/**
	 * 根据用户UID获取会员相关信息
	 *
	 * @param int $uid        	
	 */
	public function GetUserByID($uid) {
		$UserInfo = $this->find ( 'first', array (
				'conditions' => array (
						'uid' => $uid 
				) 
		) );
		if ($UserInfo) {
			App::import ( 'Model', 'City' );
			$City = new City ();
			$City_info = $City->GetCityByID ( $UserInfo ['User'] ['province'] );
			if ($City_info ['City'] ['name']) {
				$address .= $City_info ['City'] ['name'] . '/';
			}
			
			$City_info = $City->GetCityByID ( $UserInfo ['User'] ['city'] );
			if ($City_info ['City'] ['name']) {
				$address .= $City_info ['City'] ['name'] . '/';
			}
			
			$City_info = $City->GetCityByID ( $UserInfo ['User'] ['area'] );
			if ($City_info ['City'] ['name']) {
				$address .= $City_info ['City'] ['name'];
			}
			
			$UserInfo ['User'] ['address'] = ! empty ( $address ) ? $address : '未知';
		}
		return $UserInfo;
	}
	/**
	 * 根据邮箱获取用户数据
	 */
	public function GetEmailByID($email) {
		$UserInfo = $this->find ( 'first', array (
				'conditions' => array (
						'email' => $email 
				) 
		) );
		return $UserInfo;
	}
	
	/**
	 * 登陆
	 */
	public function Login($email, $password) {
		$UserInfo = $this->find ( 'first', array (
				'conditions' => array (
						'email' => $email,
						'password' => $password 
				) 
		) );
		return $UserInfo;
	}
	
	/**
	 * 添加会员
	 *
	 * @param array $data        	
	 */
	public function add($data) {
		if (! is_array ( $data )) {
			return false;
		}
		$this->begin ();
		
		$UserInfo = $this->save ( $data );
		if (! $UserInfo) {
			$this->rollback ();
			return false;
		}
		// 初始化用户专辑
		App::import ( 'Model', 'UserAlbum' );
		$UserAlbumModel = new UserAlbum ();
		$data ['UserAlbum'] ['uid'] = $UserInfo ['User'] ['uid'];
		$data ['UserAlbum'] ['name'] = '默认专辑';
		$data ['UserAlbum'] ['tags'] = '';
		$AlbumInfo = $UserAlbumModel->add ( $data );
		if (! $AlbumInfo) {
			$this->rollback ();
			return false;
		} else {
			$this->commit ();
			return true;
		}
	}
	
	/**
	 * 编辑会员
	 *
	 * @param array $data        	
	 */
	public function edit($data) {
		if (! is_array ( $data )) {
			return false;
		}
		
		if ($this->save ( $data )) {
			return true;
		}
		return false;
	}
	public function del($uid) {
		if ($this->delete ( $uid )) {
			return true;
		}
		return false;
	}
	
	/**
	 * 取得会员列表
	 *
	 * @param array $conditions
	 *        	查询条件
	 * @param int $limit
	 *        	记录数
	 */
	public function GetUserByList($conditions = array(), $limit = 10) {
		$result = array ();
		
		$result ['result'] = $this->find ( 'all', array (
				'conditions' => $conditions,
				'limit' => $limit,
				'order' => 'uid DESC' 
		) );
		
		$result ['count'] = $this->find ( 'count', array (
				'conditions' => $conditions 
		) );
		
		return $result;
	}
}
?>